// vipcard/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    headshow:0,
    equity_show:false,
    swiper_info: [],
    showPay: false,
    select: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    const res = wx.getSystemInfoSync()
    var statusbarH = res.statusBarHeight * 2
    this.setData({
      statusbar: statusbarH
    })
    getApp().page.onLoad(this, t);
    this.getList()
  },
  getList() {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    getApp().request({
      url: getApp().api.vipcard.index,
      method: "get",
      data: {},
      success: function(t) {
        wx.hideLoading()
        if (t.code == 0) {
          that.setData({
            swiper_list: t.data.list,
            swiper_info: t.data.list[0].pay_status == 1 ? t.data.list[0].pay_info : t.data.list[0]
          })
        }
      },
      complete: function() {
        wx.stopPullDownRefresh();
      }
    })
  },
  onPageScroll: function (e) {
    let opacity = 0
    if (e.scrollTop <= 400) {
      opacity = e.scrollTop / 400
    } else {
      opacity = 1
    }
    this.setData({
      headshow: opacity
    })
  },
  bindchange(e) {
    var list = this.data.swiper_list
    this.setData({
      current: e.detail.current,
      swiper_info: list[e.detail.current].pay_status == 1 ? list[e.detail.current].pay_info : list[e.detail.current]
    })
  },
  BackPage(){
    wx.navigateBack()
  },
  // EquityShow(){
  //   var equity_show =  !this.data.equity_show 
  //   this.setData({
  //     equity_show:equity_show
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().page.onShow(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectTime (t) {
    this.setData({
      select: t.currentTarget.dataset.type
    })
  },
  submitPay () {
    var current = this.data.current
    var swiper_list = this.data.swiper_list
    var swiper = swiper_list[current]
    if (swiper.pay_status == 1) {
      wx.showToast({
        icon: 'none',
        title: '已购买权益，请勿重复购买'
      })
    } else {
      if (swiper.pay_type == 2) {
        this.setData({
          showPay: true,
          select: ''
        })
      } else {
        this.submitOrder({
          id: swiper.id
        })
      }
    }
  },
  // 选择会员卡类型 月 季 年
  payTypeSubmit () {
    var current = this.data.current
    var swiper_list = this.data.swiper_list
    var swiper = swiper_list[current]
    this.submitOrder({
      id: swiper.id,
      type: this.data.select,
      showPay: false
    })
  },
  // 提交订单
  submitOrder(param) {
    wx.showLoading({
      title: '提交中',
    })
    var that = this
    getApp().request({
      url: getApp().api.vipcard.submit,
      method: "get",
      data: param,
      success: function(t) {
        wx.hideLoading()
        if (t.code == 0) {
          that.payData(t.data.order_list, t.data.pay_list)
        } else {
          wx.showToast({
            icon: 'none',
            title: t.msg
          })
        }
      },
      complete: function() {
        wx.stopPullDownRefresh();
      }
    })
  },
  // 处理支付方式
  payData (order_id, pay_list) {
    var payment = 0, that = this;
    if (pay_list.length == 1) {
      payment =  pay_list[0].payment
    } else if (pay_list.length > 1) {
      this.setData({
        order_id: order_id,
        show_pay_list: true,
        showPay: false,
        pay_list: pay_list
      })
      return false
    }
    this.setData({
      payment: payment,
      order_id: order_id,
      showPay: false
    }, function () {
      that.payDataSubmit()
    })
  },
  // 选择支付方式
  payToSubmit (t) {
    var payment = t.currentTarget.dataset.payment, that = this
    this.setData({
      payment : payment ? payment : 0
    }, function() {
      that.payDataSubmit()
    })
  },
  // 提交支付
  payDataSubmit() {
    var order_id = this.data.order_id, payment = this.data.payment,that = this
    this.closePayBox()
    getApp().request({
      url: getApp().api.vipcard.pay_data,
      method: "get",
      data: {
        order_id: order_id,
        payment: payment
      },
      success: function(e) {
        wx.hideLoading()
        if (e.code == 0) {
          if (payment == 0) {
            getApp().core.requestPayment({
              appId: e.data.appId,
              timeStamp: e.data.timeStamp,
              nonceStr: e.data.nonceStr,
              package: e.data.package,
              signType: e.data.signType,
              paySign: e.data.paySign,
              success: function(e) {},
              fail: function(e) {},
              complete: function(e) {
                  "requestPayment:fail" != e.errMsg && "requestPayment:fail cancel" != e.errMsg ? that.getList()
                  : getApp().core.showModal({
                      title: "提示",
                      content: "订单尚未支付",
                      showCancel: !1,
                      confirmText: "确认",
                      success: function(e) {
                          e.confirm && that.getList();
                      }
                  });
              }
            })
          }else {
            wx.showToast({
              icon: 'none',
              title: e.msg
            })
            setTimeout(() => {
              that.getList()
            }, 1500);
          }
        } else {
          wx.showToast({
            icon: 'none',
            title: e.msg
          })
        }
      },
      complete: function() {
        wx.stopPullDownRefresh();
      }
    })
  },
  closePayBox () {
    this.setData({
      select: '',
      showPay: false,
      show_pay_list: false
    })
  },
  toInfo (t) {
    var id = t.currentTarget.dataset.id
    wx.navigateTo({
      url: '/vipcard/info/index?id=' + id,
    })
  }
})